﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcContrib.FluentHtml.Elements;
using System.Web.Mvc;
using MShop.Core.Domain;
using MvcContrib.UI.Html;
using MvcContrib.Sorting;
using MvcContrib.FluentHtml;
using System.Web.Mvc.Html;
using MShop.Core;
using MShop.Web.Areas.Admin.Controllers;

namespace MShop.Web.Components.Extensions
{
    public static class HtmlExtensions2
    {
        

        public  static RightFlags GetRightFlags(this HtmlHelper html)
        {
            string requestUrl = string.Empty;
            SecureController controller = null;      
            if (html.ViewDataContainer is ViewUserControl)
            {
                ViewUserControl vc = (ViewUserControl)html.ViewDataContainer;
                controller = vc.ViewContext.Controller as SecureController;
                string controllername = vc.ViewContext.RouteData.Values["controller"].ToString();
                string area = vc.ViewContext.RouteData.DataTokens["area"] != null ? vc.ViewContext.RouteData.DataTokens["area"].ToString() : string.Empty;
                string action = vc.ViewContext.RouteData.Values["action"].ToString();
                if (area == string.Empty)
                {
                    requestUrl = string.Concat("/",controllername, "/", action);
                }
                else
                {
                    requestUrl = string.Concat("/",area, "/", controllername, "/", action);
                }
            }
            else if (html.ViewDataContainer is ViewPage)
            {
                ViewPage vc = (ViewPage)html.ViewDataContainer;
                controller = vc.ViewContext.Controller as MShop.Web.Areas.Admin.Controllers.SecureController;
                string controllername = vc.RouteData.Values["Controller"].ToString();
                string area = vc.RouteData.DataTokens["area"] != null ? vc.RouteData.DataTokens["area"].ToString() : string.Empty;
                string action = vc.RouteData.Values["action"].ToString();

              
                if (area == string.Empty)
                {
                    requestUrl = string.Concat("/", controllername, "/", action);
                }
                else
                {
                    requestUrl = string.Concat("/", area, "/", controllername, "/", action);
                }


            }
            return controller.UserRightsService.CurrentUserRightFlags(requestUrl);
          
            
        }

        public static MvcHtmlString SetButtonRights(this HtmlHelper html, string text, RightFlags flags,  object htmlAttributes,ButtonType buttonType=ButtonType.Submit)
        {
            TagBuilder tag = new TagBuilder("button");                
            tag.SetInnerText(text);
            if (buttonType == ButtonType.Submit)
            {
                tag.Attributes.Add("type", "submit");
            }
            else
            {
                tag.Attributes.Add("type", "button");
            }
            var _htmlAttributes = new ViewDataDictionary(htmlAttributes);
            tag.MergeAttributes<string, object>(_htmlAttributes);

             var _RightFlags=GetRightFlags(html);

             if ((_RightFlags & flags)!=0)
             {
                 return MvcHtmlString.Create(tag.ToString());                
             }
             else
             {
                 return MvcHtmlString.Empty;
             }  
        }

        public static MvcHtmlString SetButtonRights(this HtmlHelper html, string text, RightFlags flags,   ButtonType buttonType = ButtonType.Submit)
        {
            return html.SetButtonRights(text, flags, null, buttonType);
        }


        //public static MvcHtmlString SetActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        //{
        //    RightFlags flags = RightFlags.NoRight;


        //    return html.SetActionLink(linkText, actionName, controllerName, flags, routeValues, htmlAttributes);
        //}


        public static MvcHtmlString SetActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, RightFlags flags, object routeValues, object htmlAttributes)
        {
            var _RightFlags = GetRightFlags(html);
           // HttpContext.Current.Response.Write(_RightFlags.ToString());
            if ((_RightFlags & flags) != 0)
            {
                return html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);

            }
            else
            {
                return MvcHtmlString.Empty;
            }  
        }


        public static MvcHtmlString SetActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, RightFlags flags)
        {
            return html.SetActionLink(linkText, actionName, controllerName, flags, null, null);
        }

        public static MvcHtmlString SetActionLink(this HtmlHelper html, string linkText, string actionName, RightFlags flags)
        {
            return html.SetActionLink(linkText, actionName, null, flags, null, null);
        }

        public static MvcHtmlString SetActionLink(this HtmlHelper html, string linkText, string actionName, RightFlags flags, object routeValues, object htmlAttributes)
        {
            var _RightFlags = GetRightFlags(html);
           
            if ((_RightFlags & flags) != 0)
            { 
                return html.ActionLink(linkText, actionName, null, routeValues, htmlAttributes);

            }
            else
            {
                return MvcHtmlString.Empty;
            }  
        }

    }
}